package com.my.up.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.my.up.entity.UpShow;

import util.ConnManager;

public class UpDaoImpl implements UpDao {

	@Override
	public boolean addOneUp(UpShow upShow) {
		boolean flag=false;
		Connection conn=null;
		PreparedStatement pst=null;
		try{
		conn=ConnManager.getConnection();
		String sql="insert into up (u_id,s_id) values(?,?)";
		pst=conn.prepareStatement(sql);
		pst.setInt(1, upShow.getuId());
		pst.setInt(2, upShow.getsId());
		pst.execute();
		pst.close();
		conn.close();
		flag=true;
		}catch(Exception e){
			try {
				pst.close();
			} catch (Exception e2) {
				System.out.println("断开连接出错"+e2.getMessage());
			}
			try {
				conn.close();
			} catch (Exception e3) {
				System.out.println("断开连接出错"+e3.getMessage());
			}
			System.out.println("增加点赞记录出错");
			flag=false;
		}
		return flag;
	}

	@Override
	public boolean deleteOneUp(UpShow upShow) {
		boolean flag=false;
		Connection conn=null;
		PreparedStatement pst=null;
		try{
			conn=ConnManager.getConnection();
			String sql="delete from up where u_id=? and s_id=?";
			pst=conn.prepareStatement(sql);
			pst.execute();
			flag=true;
		}catch(Exception e){
			try {
				pst.close();
			} catch (Exception e2) {
				System.out.println("断开连接出错"+e2.getMessage());
			}
			try {
				conn.close();
			} catch (Exception e3) {
				System.out.println("断开连接出错"+e3.getMessage());
			}
			System.out.println("取消点赞出错");
			flag=false;
		}
		return flag;
	}

	@Override
	public List<UpShow> selectOneUp(UpShow upShow) {
		List<UpShow> upList=new ArrayList<UpShow>();
		UpShow upShow2=null;
		Connection conn=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		try{
			conn=ConnManager.getConnection();
			String sql="select u_id,u_nick from user,up where up.u_id=user.u_id and s_id=?";
			pst=conn.prepareStatement(sql);
			pst.setInt(1,upShow.getsId());
			rs=pst.executeQuery();
			
		while(rs.next()){
			upShow2.setsId(upShow.getsId());
			upShow2.setuId(rs.getInt("u_id"));
			upShow2.setuNick(rs.getString("u_nick"));
			upList.add(upShow2);
		}
		rs.close();
		pst.close();
		conn.close();
		} catch (Exception e) {
			
			try {
				rs.close();
			} catch (Exception e1) {
				System.out.println("断开连接出错"+e.getMessage());
			}
			try {
				pst.close();
			} catch (Exception e2) {
				System.out.println("断开连接出错"+e2.getMessage());
			}
			try {
				conn.close();
			} catch (Exception e3) {
				System.out.println("断开连接出错"+e3.getMessage());
			}
			System.out.println("查询点赞出错"+e.getMessage());
			
		}
		return upList;
	}

}
